

jQuery(function ($) {
    var user_info = JSON.parse(localStorage.getItem('user_info'));
    if (!user_info) {
        window.location.href = "/login/login.html";
    }
    function time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace('T', ' ');
    }
    var id = user_info.id;
    var onoff = false;
    var pageOnOff=true;
    //我的订单
    function myOrdertwbsPagination(total) {
        //分页按钮
        Page({
			num:Math.ceil(total / 30),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    myOrder(page);
                    $('#table1').html('');
                }
			}
		});
    }
    myOrder(1);
    function myOrder(page) {
        if (id) {
            var param={
                userId:id,
                type:0,
                page:page,
                size:20
            }
            send_ajax('get', '/api/order/myOrder', param, function (data) {                
                if (data.code == 200 && data.data.orderList) {
                    if(pageOnOff){
                        myOrdertwbsPagination(data.data.total);
                        pageOnOff=false;
                    };
                    var data = data.data.orderList;
                    console.log(data)
                    $('.myOrderGoods').hide();
                    myOrderList(data, $('#table1'));
                } else {
                    $('.myOrderGoods').show();
                }
            });
        }
        //订单查询切换
        $('.order_nav li').click(function () {
            var index = $(this).index();
            $(this).addClass("border_bottom_color").siblings().removeClass("border_bottom_color");
            $(".mycomment_table").eq(index).addClass('open').siblings().removeClass("open");
        });
        // $('.hide').remove()
    };
    //确认收货
    $(document).on('click', '.confirm_goods', function () {
        var order_id = $(this).attr('data-delivery-id');
        var param = {
            "orderId": order_id
        };
        send_ajax('post', '/api/order/goods_confirm', param, function (data) {
            if (data.code == 200) {
                $('#table1 tbody').remove();
                $('#table2 tbody').remove();
                $('#table3 tbody').remove();
                $('#table4 tbody').remove();
                myOrder(1);
                Unpaid(1);
                Unreceived(1);
            }
        });
    });
    //取消订单
    $(document).on('click', '.cancel_order', function () {
        var order_id = $(this).attr('data-order-id');
        var param = {
            "Order": {
                "id": order_id,
                "order_status": "3"
            },
            "Tag": "Order"
        };
        send_ajax('post', '/put', param, function (data) {
            if (data.code == 200) {
                $('#table1 tbody').remove();
                $('#table2 tbody').remove();
                $('#table3 tbody').remove();
                $('#table4 tbody').remove();
                myOrder(1);
                Unpaid(1);
                Unreceived(1);
            }
        });
    });

    //待付款
    function UnpaidtwbsPagination(total) {
        Page({
			num:Math.ceil(total / 10),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_c'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    Unpaid(page);
                    $('#table2').html('');
                }
			}
		});
    }
    Unpaid(1);
    var UnpaidOnOff=true;
    function Unpaid(page) {
        var param={
            userId:id,
            type:1,
            page:page,
            size:10
        };
        send_ajax('get', '/api/order/myOrder', param, function (data) {
            if (data.code == 200 && data.data.orderList.length) {
                $('#Unpaid').text(data.data.total);
                if(UnpaidOnOff){
                    UnpaidtwbsPagination(data.data.total);
                    UnpaidOnOff=false;
                }
                var data = data.data.orderList;
                $('.noGoods').hide();
                myOrderList(data, $('#table2'));
            } else {
                $('.noGoods').show();
                $('#Unpaid').text(0);
            }
        });
        // $('.hide').remove();
    };
    //待收货//暂时没用
    function UnreceivedtwbsPagination(total) {
        //分页按钮
        $('#pagination_c').twbsPagination({
            totalPages: Math.ceil(total / 10),
            visiblePages: Math.ceil(total / 10) < 5 ? Math.ceil(total / 10) : 5,
            first: "首页",
            last: "末页",
            prev: '上一页',
            next: '下一页',
            onPageClick: function onPageClick(event, page) {
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    Unpaid(page);
                    $('#table2').html('');
                }
            }
        });
    }
    Unreceived(1);
    function Unreceived(page) {
        if (id) {
            var param={
                userId:id,
                type:2,
                page:page,
                size:10
            };
            send_ajax('get', '/api/order/myOrder', param, function (data) {
                if (data.code == 200 && data.data.orderList) {
                    var data = data.data.orderList;
                    $('.receivedGoods').hide();
                    $('#Unreceived').text(data.length);
                    myOrderList(data, $('#table3'));
                } else {
                    $('.receivedGoods').show();
                }
            });
        }
    };
    //待评价
    Unevaluated();
    function Unevaluated() {
        if (id) {
            var param={
                userId:id,
                type:3,
                page:0,
                size:10
            };
            send_ajax('get', '/api/order/myOrder', param, function (data) {
                if (data.code == 200&&data.data.orderList) {
                    var data = data.data.orderList;
                    $('#Unevaluated').text(data.length);
                    myEvaluation(data, $('#table4'));
                    $('.evaluated').hide();
                }
            });
        }
    };
    //渲染我的订单列表
    function myOrderList(data, el) {
        // var html = "";
        // var orderDeliveryArr = [];
        // var orderGoodsArr = [];
        //处理是否可以取消订单
        // for (var _i = 0; _i < data.length; _i++) {
        //     //循环的订单
        //     var a_order = data[_i].order;
        //     var a_order_deliverys = data[_i].goodsList;
        //     if(a_order_deliverys.length){
        //         var current_deliveryStatus = 0;
        //         for (var _n = 0; _n < a_order_deliverys.length; _n++) {
        //             // var a_order = a_order_deliverys[_n].orderDelivery;
        //             var a_orderStatus = a_order_deliverys[_n].deliveryStatus;
        //             if (a_orderStatus != 0) {
        //                 current_deliveryStatus = a_orderStatus;
        //                 break;
        //             }
        //         }
        //         a_order.is_can_cancle = current_deliveryStatus == 0; 
        //     }
        // };
        var orderHtml="";
        for (var i = 0; i < data.length; i++) {
            var a_order = data[i].order;
            var odgList = data[i].goodsList;
            orderHtml+='<tbody data-order-id=' + a_order.id + '>\
                            <tr class="sep-row"></tr>\
                            <tr class="tr-th">\
                                <td colspan="5">\
                                    <span>' +time(a_order.create_time)+ '</span>\
                                    <span>\u8BA2\u5355\u53F7\uFF1A' + a_order.order_sn + '</span>\
                                    <span>普通订单</span>\
                                </td>\
                            </tr>\
                            <tr>\
                                <td colspan="4">\
                                    <table class="myOrders" data-order-id=' + a_order.id + ' colspan="4">'      
                // for (var n = 0; n < odgList.length; n++) {
                    // var a_order = odgList[n].orderDelivery
                    // var a_order = odgList[n]
                    // orderHtml+='<tbody  data-orderDelivery-id=' +a_order.id + ' class="delivery" colspan="4">'
                    // var a_goodslist = odgList[n].orderGoods;
                    //查看物流
                    // <p class="' + (a_goods.deliveryStatus != 1 ? 'open' : 'hide') + '">\n <a href="/user/logistics_inquiry.html?Logistics=' + (a_goods.logisticsCode ? a_goods.logisticsCode : " ") + '&&delivery_sn=' + a_goods.delivery_sn + '" target="_blank">\u67E5\u770B\u7269\u6D41</a></p></div></td>\
                    // <p class="' + (a_goods.refund_status != 0 ? 'open' : 'hide') + '"><a class="refunding"><span class="' + (a_goods.refund_status == 1 ? 'open' : 'hide') + '">\u9000\u6B3E\u4E2D</span>\<span class="' + (a_goods.refund_status == 2 ? 'open' : 'hide') + ('">\u5DF2\u9000\u6B3E</span>\n</a>\n</p>\

                    for(var k = 0;k<odgList.length;k++){
                        var a_goods = odgList[k];
                        orderHtml+='<tbody  data-orderDelivery-id= class="delivery" colspan="4"><tr class="tr-bd ">\
                        <td class="goods_p">\
                            <div class="goods_item clearfix">\
                                <div class="goods_pic">\
                                    <a href="/goods-' + a_goods.id + '.html" target="_blank">\
                                        <img src="' + (a_goods.thumb ? isImgHttp(a_goods.thumb) : '/asset/image/tu4.jpg') + '" alt="">\
                                    </a>\n</div>\
                                <div class="goods_msg">\
                                    <div class="goods_name"><a href="/goods-' + a_goods.id + '.html" target="_blank">' + a_goods.goodsName + '</a></div></div></div></td>\
                        <td class="goods_r">\<div class="txt">'+a_order.receiver+'</div>\n</td>\
                        <td class="goods_s">\
                            <div class="status">\
                                <p class="' + (  a_order.paid_status != 0 &&  a_order.order_status != 3&&  a_order.order_status == 1 ? 'open' : 'hide') + '">待收货</p>\
                                <p class="' + (  a_order.paid_status != 0 &&  a_order.order_status != 3&&  a_order.order_status == 2 ? 'open' : 'hide') + '">已发货</p>\
                                <p class="' + (  a_order.paid_status != 0 && a_order.order_status == 5 ? 'open' : 'hide') + '">已收货</p>\
                                <p class="' + (a_order.order_status == 3 ? 'open' : 'hide') + '">已取消</p>\
                                <p class="' + ( a_order.paid_status == 0 && a_order.order_status != 3 ? 'open' : 'hide') + '">未支付</p>\
                                <p class="' + (  a_order.paid_status == 1 && a_order.order_status == 7 ? 'open' : 'hide') + '">退货中</p>\
                                <p class="' + (  a_order.paid_status == 1 && a_order.order_status ==8 ? 'open' : 'hide') + '">退货完成</p>\
                                <p><a href="/user/order_details.html?order=' + a_order.id + '" target="_blank">订单详情</a></p>\
                            <td class="goods_o">\
                                <div class="operate">\
                                    <p class="' + (  a_order.paid_status != 0 &&  a_order.order_status != 3&&  a_order.order_status == 1 ? 'open' : 'hide') + '">待收货</p>\
                                    <p class="' + (  a_order.paid_status != 0 &&  a_order.order_status != 3&&  a_order.order_status == 2 ? 'open' : 'hide') + '">已发货</p>\
                                    <p class="' + (  a_order.paid_status != 0 && a_order.order_status == 5 ? 'open' : 'hide') + '">已收货</p>\
                                    <p class="' + (  a_order.paid_status == 1 && a_order.order_status == 7 ? 'open' : 'hide') + '">退货中</p>\
                                    <p class="' + (  a_order.paid_status == 1 && a_order.order_status ==8 ? 'open' : 'hide') + '">退货完成</p>\
                                    <p class="' + (   a_order.paid_status != 0 && a_order.order_status == 2 ? 'open' : 'hide') + '">\n <a class="confirm_goods" data-delivery-id="' + a_goods.orderId + '">确认收货</a>\n</p>\
                                    <p class="' + (  a_order.paid_status == 0 && a_order.order_status != 3 ? 'open' : 'hide') + ' ">\n <a href="/goods/order_payment.html?order_sn=' + a_order.order_sn + '" class="confirm">去支付</a>\n</p>\
                                    <p class="' + (a_order.order_status == 6? 'open' : 'hide') + '"> 已确认收货</p>\
                                    <p class="' + (a_order.order_status == 3 ? 'open' : 'hide') + '">已取消</p>\
                                </div>\
                            </td>\
                                </tr></tbody>'

                    }
                // };

            orderHtml+='</tr></table></td><td>\n<table class="my_price">\n<tr>\n <td>\n<div class="amount">\n<p>\总额' + a_order.total_amt + '</p>\n<p>在线支付</p>\n <p class="' + (a_order.is_can_cancle == 0 ? 'open' : 'hide')+ '"><a href="/user/user_service.html?order=' + a_order.id + '" class="after_sale" target="_blank">申请售后</a></p>\n                                    <p class="' + (a_order.order_status == 3 ? 'open' : 'hide') + '">\n                                        <a data-order-id="' + a_order.id + '">已取消</a>\n                                    </p>\n                                    <p class="' + (a_order.order_status == 5 ? 'open' : 'hide') + '">\n                                        <a href="/user/user_evaluate.html?order_id=' + a_order.id + '" class="confirm" data-order-id="' + a_order.id + '" target="_blank">评价晒单</a>\n                                    </p>\n                                    <p class="' + ( a_order.order_status<3? 'open' : 'hide') + '">\n                                        <a class="confirm cancel_order" data-order-id="' + a_order.id + '">取消订单</a>\n                                    </p>\n                                </div>\n                            </td>\n                        </tr>\n                    </table>\n                </td>\n            </tr>\</tbody>'
        };
        el.append(orderHtml);
       
         var orderGoodsDelivery = el.find('.delivery'); //渲染商品
        // //合并单元格
        for (var c = 0; c < orderGoodsDelivery.length; c++) {
            if ($(orderGoodsDelivery[c]).children().length >= 2) {
                var tr = $(orderGoodsDelivery[c]).find('tr')[0];
                $(tr).find('td')[1].setAttribute('rowSpan', $(orderGoodsDelivery[c]).children().length);
                $(tr).find('td')[2].setAttribute('rowSpan', $(orderGoodsDelivery[c]).children().length);
                $(tr).find('td')[3].setAttribute('rowSpan', $(orderGoodsDelivery[c]).children().length);
                var trs = $(orderGoodsDelivery[c]).find('tr').slice(1);
                if (isIE() || isIE11()) {
                    for (var _k = 0; _k < trs.length; _k++) {
                        $(trs[_k]).find('td').not(":eq(0)").removeNode(true);
                    }
                } else {
                    for (var _k2 = 0; _k2 < trs.length; _k2++) {
                        $(trs[_k2]).find('td').not(":eq(0)").remove();
                    }
                }
            }
        };
    };
    //渲染待评价的页面
    function myEvaluation(data, el) {
        var html = "";
        var orderDeliveryArr = [];
        var orderGoodsArr = [];
        for (var i = 0; i < data.length; i++) {
            var a_order = data[i].order;
            var odgList = data[i].goodsList;
            //渲染订单号
            // html += ' <tbody data-order-id=' + data[i].Order.id + '><tr class="sep-row"></tr><tr class="tr-th"><td colspan="4"><span>' + data[i].Order.create_time.split('.')[0] + '</span>\n                    <span>\u8BA2\u5355\u53F7\uFF1A' + data[i].Order.order_sn + '</span>\n</td>\n </tr>\n<tr>\n<td colspan="3">\n <table class="myOrders" data-order-id=' + data[i].Order.id + ' colspan="4">\n                        \n                    </table>\n                </td>\n';
            html+='<tbody data-order-id=' + a_order.id + '>\
                        <tr class="sep-row"></tr>\
                        <tr class="tr-th">\
                            <td colspan="4">\
                                <span>' + time(a_order.create_time) + '</span>\
                                <span>\u8BA2\u5355\u53F7\uFF1A' + a_order.order_sn + '</span>\
                            </td>\
                        </tr>\
                        <tr>\
                            <td colspan="3">\
                                <table class="myOrders" data-order-id=' + a_order.id + ' colspan="4">'


            // for(var k = 0;k<odgList.length;k++){
            //     var a_order = odgList[k].orderDelivery;
            //     var a_goodslist = odgList[k].orderGoods;
            //      html+='<tbody class=" delivery" data-orderDelivery-id=' +a_order.id + ' colspan="4">'
                for(var n = 0;n<a_goodslist.length;n++){
                    var a_goods = a_goodslist[n];
                    html+='<tbody class=" delivery" data-orderDelivery-id=' +a_order.id + ' colspan="4">\
                            <tr class="tr-bd">\
                                    <td class="goods_p" style="width:550px;">\
                                        <div class="goods_item clearfix">\
                                            <div class="goods_pic">\
                                                <a href="/goods-'+a_goods.id+'.html" target="_blank">\
                                                    <img src="' + isImgHttp(a_goods.thumb) + '" alt="">\
                                                </a>\
                                            </div>\
                                            <div class="goods_msg">\
                                                <div class="goods_name"><a href="/goods-'+a_goods.id+'.html" target="_blank">' + a_goods.goodsName + '</a>\</div>\
                                            </div>\
                                        </div>\
                                    </td>\
                                    <td class="goods_r">\
                                        <div class="txt">\ ' + a_order.receiver + '\n</div>\
                                    </td>\
                                    <td class="goods_o">\
                                        <div class="operate">\
                                            <p><a href="/user/user_evaluate.html?order=' + a_order.id + '" class="confirm appraise" target="_blank">\u8BC4\u4EF7</a></p>\
                                            <p><a href="/user/order_details.html?order=' + a_order.id + '" class="after_sale" target="_blank">\u8BA2\u5355\u8BE6\u60C5</a>\</p>\
                                        </div>\
                                    </td>\
                                </tr>\
                            </tbody>'

                }
            // }


                    html+='</table> </td>\
                                <td>\
                                    <table class="my_price">\
                                                <tr>\
                                                    <td>\
                                                        <div class="amount">\
                                                            <p>\u603B\u989D \xA5' + a_order.total_amt + '</p>\
                                                            <p> 在线支付</p>\
                                                        </div>\
                                                    </td>\
                                                </tr>\
                                    </table>\
                                </td>\
                            </tr>\
                        </tbody>'
           
        };
        el.append(html);
        var myOrder = el.find('.myOrders');

         var orderGoodsDelivery = el.find('.delivery'); //渲染商品
        for (var c = 0; c < orderGoodsDelivery.length; c++) {
            if ($(orderGoodsDelivery[c]).children().length >= 2) {
                var tr = $(orderGoodsDelivery[c]).find('tr')[0];
                $(tr).find('td')[1].setAttribute('rowSpan', $(orderGoodsDelivery[c]).children().length);
                $(tr).find('td')[2].setAttribute('rowSpan', $(orderGoodsDelivery[c]).children().length);
                var trs = $(orderGoodsDelivery[c]).find('tr').slice(1);
                if (isIE() || isIE11()) {
                    for (var _k3 = 0; _k3 < trs.length; _k3++) {
                        $(trs[_k3]).find('td').not(":eq(0)").removeNode(true);
                    }
                } else {
                    for (var _k4 = 0; _k4 < trs.length; _k4++) {
                        $(trs[_k4]).find('td').not(":eq(0)").remove();
                    }
                }
            }
        }
    };
    function isIE() {
        if (!!window.ActiveXobject || "ActiveXObject" in window) {
            return true;
        } else {
            return false;
        }
    };
    function isIE11() {
        if (/Trident\/7\./.test(navigator.userAgent)) {
            return true;
        } else {
            return false;
        }
    }
});